﻿/*
 * Copyright 2010 Marc Cesarine
 * 
 * This file is part of Ssh Commander.

 * Ssh Commander is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * Ssh Commander is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with Ssh Commander.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Data;
using Microsoft.Win32;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;

namespace SshCommander
{
    /// <summary>
    /// Logique d'interaction pour General.xaml
    /// </summary>
    public partial class General : System.Windows.Window
    {
        #region variables

        private ServersList serversList;

        #endregion

        #region constructor

        public General()
        {
            InitializeComponent();
            serversList = new ServersList();
            ObjectDataProvider ServeursListDataProvider = this.FindResource("ServeursList") as ObjectDataProvider;
            ServeursListDataProvider.ObjectInstance = serversList.serversList;
        }

        #endregion

        #region methods

        private void ListImportBoxItem_Selected(object sender, RoutedEventArgs e)
        {
            ContentGrid.Children.Clear();
            ListImport listImportView = new ListImport(ref serversList);
            ContentGrid.Children.Add(listImportView);
        }

        private void ExitMenuItem_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void ConnectivityBoxItem_Selected(object sender, RoutedEventArgs e)
        {
            ContentGrid.Children.Clear();
            Connectivity connectivityView = new Connectivity(ref serversList);
            ContentGrid.Children.Add(connectivityView);
        }

        private void AccountsListBoxItem_Selected(object sender, RoutedEventArgs e)
        {
            ContentGrid.Children.Clear();
            AccountCheck accountCheckView = new AccountCheck(ref serversList);
            ContentGrid.Children.Add(accountCheckView);
        }

        private void PasswordsResetListBoxItem_Selected(object sender, RoutedEventArgs e)
        {
            ContentGrid.Children.Clear();
            PasswordReset passwordResetView = new PasswordReset();
            ContentGrid.Children.Add(passwordResetView);
        }

        private void exportButton_Click(object sender, RoutedEventArgs e)
        {
            // Create OpenFile window object
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter = "Excel OpenDocument file (*.xls)|*.xls";
            saveFileDialog.Title = "Export reporting to Excel file";
            saveFileDialog.ShowDialog();

            // If the file name is not an empty string open it for saving.
            if ((saveFileDialog.FileName != "") && (serversList.serversList.Count > 0))
            {
                // Create Excel object
                Excel excelfile = new Excel();

                // Get template from assembly via stream
                Assembly MyAssembly = Assembly.GetExecutingAssembly();
                Stream ExcelTemplateStream = MyAssembly.GetManifestResourceStream("SshCommander.Excel.Template.xls");

                // Open excel file via stream
                excelfile.OpenFile(ExcelTemplateStream);

                // Set excel file informations
                excelfile.SetFileInfo("Ssh Comander", "Ssh Commander report");

                string filename = saveFileDialog.FileName;

                try
                {
                    // Select sheet in excelfile
                    excelfile.SelectSheet("Report");

                    // Setting the styles
                    // Setting the custom colors
                    excelfile.SetCustomColor(HSSFColor.ROSE.index, (byte)230, (byte)185, (byte)184);
                    excelfile.SetCustomColor(HSSFColor.GREEN.index, (byte)0, (byte)176, (byte)80);
                    excelfile.SetCustomColor(HSSFColor.LIME.index, (byte)215, (byte)228, (byte)188);

                    // Header style
                    HSSFCellStyle HeaderStyle = excelfile.CreateStyle();
                    HeaderStyle.FillPattern = 1;
                    HeaderStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.ROSE.index;
                    HeaderStyle.Alignment = HSSFCellStyle.ALIGN_CENTER;
                    HeaderStyle.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
                    HeaderStyle.WrapText = true;
                    HeaderStyle.BorderBottom = HSSFCellStyle.BORDER_THIN;
                    HeaderStyle.BorderLeft = HSSFCellStyle.BORDER_THIN;
                    HeaderStyle.BorderRight = HSSFCellStyle.BORDER_THIN;
                    HeaderStyle.BorderTop = HSSFCellStyle.BORDER_THIN;
                    //HSSFCellStyle HeaderStyle = hssfworkbook.GetCellStyleAt(0);

                    // Object style
                    HSSFCellStyle ObjectStyle = excelfile.CreateStyle();
                    ObjectStyle.FillPattern = 1;
                    ObjectStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LIME.index;
                    //HSSFCellStyle HeaderStyle = hssfworkbook.GetCellStyleAt(0);

                    // OK Style
                    HSSFFont OKFont = excelfile.CreateFont();
                    OKFont.Color = HSSFColor.GREEN.index;
                    OKFont.Boldweight = HSSFFont.BOLDWEIGHT_BOLD;
                    HSSFCellStyle OKStyle = excelfile.CreateStyle();
                    OKStyle.SetFont(OKFont);
                    //HSSFCellStyle OKStyle = hssfworkbook.GetCellStyleAt(1);

                    // ARecetter Style
                    HSSFFont ARecetterFont = excelfile.CreateFont();
                    ARecetterFont.Color = HSSFColor.BLUE.index;
                    ARecetterFont.Boldweight = HSSFFont.BOLDWEIGHT_BOLD;
                    HSSFCellStyle ARecetterStyle = excelfile.CreateStyle();
                    ARecetterStyle.SetFont(ARecetterFont);
                    //HSSFCellStyle ARecetterStyle = hssfworkbook.GetCellStyleAt(2);

                    // KO Style
                    HSSFFont KOFont = excelfile.CreateFont();
                    KOFont.Color = HSSFColor.RED.index;
                    KOFont.Boldweight = HSSFFont.BOLDWEIGHT_BOLD;
                    HSSFCellStyle KOStyle = excelfile.CreateStyle();
                    KOStyle.SetFont(KOFont);
                    //HSSFCellStyle KOStyle = hssfworkbook.GetCellStyleAt(3);

                    // EnCours Style
                    HSSFFont EnCoursFont = excelfile.CreateFont();
                    EnCoursFont.Color = HSSFColor.ORANGE.index;
                    EnCoursFont.Boldweight = HSSFFont.BOLDWEIGHT_BOLD;
                    HSSFCellStyle EnCoursStyle = excelfile.CreateStyle();
                    EnCoursStyle.SetFont(EnCoursFont);
                    //HSSFCellStyle EnCoursStyle = hssfworkbook.GetCellStyleAt(4);

                    // Creating header
                    HSSFCell cell = excelfile.GetCell(0,0);
                    cell.SetCellValue("Server name");
                    cell.CellStyle = HeaderStyle;
                    cell = excelfile.GetCell(0,1);
                    cell.CellStyle = HeaderStyle;
                    excelfile.MergeCells(0, 0, 0, 1);
                    cell = excelfile.GetCell(1,0);
                    cell.SetCellValue("Connectivity (Port 22)");
                    cell.CellStyle = HeaderStyle;
                    cell = excelfile.GetCell(1,1);
                    cell.CellStyle = HeaderStyle;
                    excelfile.MergeCells(1, 0, 1, 1);
                    cell = excelfile.GetCell(2,0);
                    cell.SetCellValue("Accounts");
                    cell.CellStyle = HeaderStyle;

                    int i = 2;
                    foreach (Account account in serversList.accountsList)
                    {
                        cell = excelfile.GetCell(i,1);
                        cell.SetCellValue(account.login);
                        cell.CellStyle = HeaderStyle;
                        excelfile.AddHeader(account.login, i);
                        if (i != 2)
                        {
                            cell = excelfile.GetCell(i,0);
                            cell.CellStyle = HeaderStyle;
                        }
                        i++;
                    }
                    excelfile.MergeCells(2, 0, i-1, 0);

                    // Fill cells with server names and accounts login
                    i = 2;
                    foreach (Server server in serversList.serversList)
                    {
                        cell = excelfile.GetCell(0, i);
                        cell.SetCellValue(string.Format("{0} - {1}", server.name, server.hostname));
                        cell.CellStyle = ObjectStyle;
                        cell = excelfile.GetCell(1, i);
                        cell.SetCellValue(server.teststatut.ToString());

                        switch (server.teststatut)
                        {
                            case Server.statut.OK:
                                cell = excelfile.GetCell(1, i);
                                cell.CellStyle = OKStyle;
                                break;
                            case Server.statut.KO:
                                cell = excelfile.GetCell(1, i);
                                cell.CellStyle = KOStyle;
                                break;
                            case Server.statut.EnCours:
                                cell = excelfile.GetCell(1, i);
                                cell.CellStyle = EnCoursStyle;
                                break;
                            case Server.statut.ARecetter:
                                cell = excelfile.GetCell(1, i);
                                cell.CellStyle = ARecetterStyle;
                                break;
                        }

                        foreach (Account account in server.accounts)
                        {
                            int columnID = excelfile.GetHeaderID(account.login);

                            cell = excelfile.GetCell(columnID, i);
                            cell.SetCellValue(server.teststatut.ToString());

                            switch (account.teststatus)
                            {
                                case Account.status.OK:
                                    cell = excelfile.GetCell(columnID, i);
                                    cell.CellStyle = OKStyle;
                                    break;
                                case Account.status.KO:
                                    cell = excelfile.GetCell(columnID, i);
                                    cell.CellStyle = KOStyle;
                                    break;
                                case Account.status.EnCours:
                                    cell = excelfile.GetCell(columnID, i);
                                    cell.CellStyle = EnCoursStyle;
                                    break;
                                case Account.status.ARecetter:
                                    cell = excelfile.GetCell(columnID, i);
                                    cell.CellStyle = ARecetterStyle;
                                    break;
                            }
                        }
                        i++;
                    }
                    excelfile.SaveFile(filename);

                    MessageBox.Show("Export successfully terminated.", "Export successfull");
                }
                catch (Exception)
                {
                    MessageBox.Show("An error occured during file export", "Export error");
                }

            }
        }

        #endregion
    }
}
